package com.lanju.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.lanju.dao.AllFreeDao;
import com.lanju.dao.UserDao;
import com.lanju.entity.AllFree;
import com.lanju.entity.User;
import com.lanju.service.AllFreeService;
@Service
public class AllFreeServiceImpl implements AllFreeService {
	
	@Autowired
	AllFreeDao allFreeDao;
	@Autowired
	UserDao userDao;

	@Override
	@Transactional(value="financialmarketingTM", rollbackFor = Exception.class)
	public Map<String, Object> saveFree(Map<String, Object> map) {
		Map<String,Object> result = new HashMap<String, Object>();
		try {
			AllFree af = new AllFree();
			JSONObject json = JSONObject.parseObject(JSONObject.toJSONString(map));
			af = json.toJavaObject(AllFree.class);
			af.setCreate_time(new Date());
			af.setCreator(map.get("creator").toString());
			User u =userDao.getById(af.getUserId());
			af.setUserName(u.getRealName()==null?u.getNickName():u.getRealName());
			af.setMonth(new Date());
			allFreeDao.saveFree(af);
			result.put("success", true);
			result.put("msg", "新增成功");
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success", false);
			result.put("msg", "新增失败");
			return result;
		}
	}

	@Override
	@Transactional(value="financialmarketingTM", rollbackFor = Exception.class)
	public Map<String, Object> updateFree(Map<String, Object> map) {
		Map<String,Object> result = new HashMap<String, Object>();
		JSONObject json = JSONObject.parseObject(JSONObject.toJSONString(map));
		try {
			AllFree af = json.toJavaObject(AllFree.class);
			User u =userDao.getById(af.getUserId());
			af.setUserName(u.getRealName()==null?u.getNickName():u.getRealName());
			af.setMonth(new Date());
			allFreeDao.updateFree(af);
			result.put("success", true);
			result.put("msg", "修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success", false);
			result.put("msg", "修改失败");
		}
		return result;
	}

	@Override
	@Transactional(value="financialmarketingTM", rollbackFor = Exception.class)
	public Map<String, Object> deleteFree(String id) {
		Map<String,Object> result = new HashMap<String, Object>();
		try {
			allFreeDao.deleteFree(id);
			result.put("success", true);
			result.put("msg", "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success", false);
			result.put("msg", "删除失败");
		}
		return result;
	}

	@Override
	public Map<String, Object> getAllFreeById(String id) {
		Map<String,Object> result = new HashMap<String, Object>();
		try {
			AllFree af = allFreeDao.getAllFreeById(id);
			result.put("rows", af);
			result.put("msg", "获取成功");
			result.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			result.put("rows", null);
			result.put("msg", "获取失败");
			result.put("success", false);
		}
		return result;
	}

	@Override
	public Map<String, Object> getAllFreeList(Map<String,Object> map) {
		Map<String,Object> result = new HashMap<String, Object>();
		try {
			result.put("rows", allFreeDao.getAllFreeList(map));
			result.put("total", allFreeDao.getAllFreeListCount(map));
			result.put("msg", "获取成功");
			result.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			result.put("msg", "获取失败");
			result.put("success", false);
		}
		return result;
	}

	@Override
	public Map<String, Object> getUserList(Map<String, Object> param) {
		Map<String, Object> map=new HashMap<>();
		try {
			map.put("user", allFreeDao.getAllUserList(param));
		} catch (Exception e) {
			map.put("rows", null);
			e.printStackTrace();
		}
		return map;
	}
	
	

}
